package Test;

import mine.DaoJu;
import mine.Fish;
import mine.FishUI;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import javax.swing.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.*;


@RunWith(Parameterized.class)
public class FishUITest {
    FishUI aFishUI;
    @Before
    public void setup(){
        aFishUI = new FishUI();
    }

    @After
    public void teardown(){
        aFishUI = null;
    }

    @org.junit.Test
    public void setGamebutton() {
        aFishUI.setGameButton(true);

        for (JButton b :aFishUI.getGameButton()){
            assertTrue(b.isVisible());
        }
    }

    @org.junit.Test
    public void otherFishMove_1() {
        Fish aFish = new Fish(200,1,5,0);
        ArrayList<Fish> currentFishList = new ArrayList<Fish>();
        currentFishList.add(aFish);
        aFishUI.setFishList(currentFishList);
        aFishUI.otherFishMove();
        assertTrue(aFish.getX()==205);
    }

    @org.junit.Test
    public void otherFishMove_2() {
        Fish aFish = new Fish(200,1,5,1);
        ArrayList<Fish> currentFishList = new ArrayList<Fish>();
        currentFishList.add(aFish);
        aFishUI.setFishList(currentFishList);
        aFishUI.otherFishMove();
        assertTrue(aFish.getX()==195);
    }

    @org.junit.Test
    public void otherFishMove_3() {
        Fish aFish = new Fish(2001,1,5,0);
        ArrayList<Fish> currentFishList = new ArrayList<Fish>();
        currentFishList.add(aFish);
        aFishUI.setFishList(currentFishList);
        aFishUI.otherFishMove();
        assertTrue(aFish.getState()==0);
    }

    @org.junit.Test
    public void otherFishMove_4() {
        Fish aFish = new Fish(-201,1,5,1);
        ArrayList<Fish> currentFishList = new ArrayList<Fish>();
        currentFishList.add(aFish);
        aFishUI.setFishList(currentFishList);
        aFishUI.otherFishMove();
        assertTrue(aFish.getState()==0);
    }

    @org.junit.Test
    public void otherFishRemove() {
        Fish aFish = new Fish();
        aFish.setState(0);
        ArrayList<Fish> currentFishList = new ArrayList<Fish>();
        currentFishList.add(aFish);
        aFishUI.setFishList(currentFishList);
        aFishUI.otherFishRemove();
        assertTrue(aFishUI.getFishList().isEmpty());
    }

    @org.junit.Test
    public void getRangkingFile(){
        int[][] temp = aFishUI.getRangkingFile();
        assertTrue(temp[0][1]==0);
    }


    @org.junit.Test
    public void collisionTest_1() {

        Fish[][] fishData = new Fish[8][2];

        float[][] fishX = {{800,795},{800,750},{800,795},{800,750},{800,805},{800,850},{800,805},{800,850}};
        float[][] fishY = {{500,495},{500,495},{500,505},{500,505},{500,505},{500,505},{500,495},{500,495}};
        int[][] fishGrade = {{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4}};
        int[][] fishWidth = {{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20}};
        int[][] fishHeight = {{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20}};
        int[][] fishScore = {{0,2},{0,2},{0,2},{0,2},{0,2},{0,2},{0,2},{0,2}};

        for(int i=0;i<8;i++){//i:组
            for(int k=0;k<2;k++) {//k:第k条鱼
                fishData[i][k] = new Fish();
                fishData[i][k].setX(fishX[i][k]) ;
                fishData[i][k].setY(fishY[i][k]);
                fishData[i][k].setGrade(fishGrade[i][k]);
                fishData[i][k].setWidth(fishWidth[i][k]);
                fishData[i][k].setHeight(fishHeight[i][k]);
                fishData[i][k].setScore(fishScore[i][k]);
            }
            ArrayList<Fish> currentFishList = new ArrayList<Fish>();
            currentFishList.add(fishData[i][1]);//每组第二条鱼是otherfish
            aFishUI.setMyFish(fishData[i][0]);
            aFishUI.setFishList(currentFishList);
            aFishUI.collisionTest();
            aFishUI.setDaoJu(null);
            if(i%2==0)
                assertTrue(fishData[i][0].getScore()==2 && fishData[i][1].getState()==0);
            else
                assertTrue(fishData[i][0].getScore()==0 && fishData[i][1].getState()!=0);
        }

    }


    @org.junit.Test
    public void collisionTest_2() {

        Fish[] fishData = new Fish[8];
        DaoJu[] daojuList = new DaoJu[8];

        float[][] X = {{800,795},{800,750},{800,795},{800,750},{800,805},{800,850},{800,805},{800,850}};
        float[][] Y = {{500,495},{500,495},{500,505},{500,505},{500,505},{500,505},{500,495},{500,495}};
        int[][] Width = {{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20}};
        int[][] Height = {{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20},{20,20}};

        for(int i=0;i<8;i++) {//i:组

            fishData[i] = new Fish();
            fishData[i].setX(X[i][0]);
            fishData[i].setY(Y[i][0]);
            fishData[i].setWidth(Width[i][0]);
            fishData[i].setHeight(Height[i][0]);
            daojuList[i] = new DaoJu();
            daojuList[i].setX(Math.round(X[i][1]));
            daojuList[i].setY(Math.round(Y[i][1]));
            daojuList[i].setWidth(Width[i][1]);
            daojuList[i].setHeight(Height[i][1]);

            ArrayList<Fish> currentFishList = new ArrayList<Fish>();
            fishData[i].setSpeed(2);//仅用于测试 随便设的
            aFishUI.setMyFish(fishData[i]);
            aFishUI.setFishList(currentFishList);
            aFishUI.setDaoJu(daojuList[i]);
            aFishUI.collisionTest();

            if(i%2==0)//碰撞成功
                assertTrue(fishData[i].getSpeed()==3);
            else
                assertTrue(fishData[i].getSpeed()==2);
        }
    }

    @org.junit.Test
    public void collision_daoju() {
        //道具效果测试
        Fish myFish=new Fish(true);
        DaoJu daoJu=new DaoJu();
        aFishUI.setDaoJu(daoJu);
        aFishUI.setMyFish(myFish);
        aFishUI.collision_daoju();
        assertTrue(myFish.getSpeed()==13.5&&myFish.getY_speed()==13.5);
    }


    @org.junit.Test
    public void collision_1() {
        //自己鱼2级 碰撞4级鱼 score仅用于测试随便写的2
        Fish aFish = new Fish();
        aFish.setDirection(0);
        aFish.setX(1000);
        aFish.setY(1000);
        aFish.setGrade(2);
        aFish.setScore(0);
        Fish aFish1 = new Fish();
        aFish1.setDirection(0);
        aFish1.setX(1500);
        aFish1.setY(1500);
        aFish1.setGrade(4);
        aFish1.setScore(2);
        aFishUI.collision(aFish, aFish1);
        assertTrue(aFish.getScore() == 2 && aFish1.getState() == 0);
    }

    @org.junit.Test
    public void collision_2() {
        //自己鱼4级 碰撞2级鱼
        Fish aFish2 = new Fish();
        aFish2.setDirection(0);
        aFish2.setX(1000);
        aFish2.setY(1000);
        aFish2.setGrade(4);
        aFish2.setScore(0);
        aFish2.setState(1);
        Fish aFish3 = new Fish();
        aFish3.setDirection(0);
        aFish3.setX(1500);
        aFish3.setY(1500);
        aFish3.setGrade(2);
        aFish3.setScore(2);
        aFishUI.setMyFish(aFish2);
        aFishUI.collision(aFish2,aFish3);
        assertTrue(aFish2.getState()==0);
    }

    @Parameterized.Parameter(0)
    public int score;
    @Parameterized.Parameter(1)
    public int grade;
    @Parameterized.Parameter(2)
    public int width;
    @Parameterized.Parameter(3)
    public int height;
    @Parameterized.Parameter(4)
    public int predictedGrade;
    @Parameterized.Parameter(5)
    public int predictedwidth;
    @Parameterized.Parameter(6)
    public int predictedheight;


    @Parameterized.Parameters(name="{index}:testGetArea[{0}]={1}")
    public static Collection testData()
    {
        return Arrays.asList(
                new Object[][]{
                        {2,4,50,50,4,77,53},
                        {5,4,50,50,3,100,70},
                        {20,3,50,50,2,130,90},
                        {50,2,50,50,1,220,150}
                }
        );

    }

    @org.junit.Test
    public void upgrade() {

        Fish aFish = new Fish();
        aFish.setGrade(grade);
        aFish.setWidth(width);
        aFish.setHeight(height);//这里width和height都是乱写的 和真实不一样为了测试upgrade是不是正确运行
        aFish.setScore(score);
        aFishUI.setMyFish(aFish);
        aFishUI.upgrade(aFish);
        assertTrue(aFish.getGrade()==predictedGrade && aFish.getWidth()==predictedwidth && aFish.getHeight()==predictedheight);

    }

    @org.junit.Test
    public void win() {
        Fish aFish = new Fish(true);
        aFishUI.setMyFish(aFish);
        aFishUI.win();
        assertTrue(aFishUI.getMyFish().getState()==2);
    }

    @org.junit.Test
    public void fail() {
        Fish aFish = new Fish(true);
        aFishUI.setMyFish(aFish);
        aFishUI.fail();
        assertTrue(aFishUI.getMyFish().getState()==0);

    }

    @org.junit.Test
    public void myScoreToString() {
        Fish aFish = new Fish();
        aFish.setScore(45);
        aFishUI.setMyFish(aFish);
        assertTrue(aFishUI.myScoreToString().equals("your score is 45"));
    }

    @org.junit.Test
    public void gameTime_1() {
        aFishUI.setS(11);
        aFishUI.setM(10);
        assertTrue(aFishUI.gameTime().equals("10:11"));
    }

    @org.junit.Test
    public void gameTime_2() {
        aFishUI.setS(5);
        aFishUI.setM(10);
        assertTrue(aFishUI.gameTime().equals("10:05"));
    }
}